home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / TOOLBAR.C < prev    next >
C/C++ Source or Header  |  1992-09-25  |  11KB  |  407 lines

  1. /* Routines for the TOOLBAR library         */
  2. /* Copyright (C) Stephen Chung, 1991-1992.  */
  3. /* All rights reserved.                     */
  4.  
  5.  
  6. #include <windows.h>
  7. #include "toolbar.h"
  8.  
  9. #define MAXSTATES       3
  10.  
  11.  
  12. static BOOL ClassRegistered = FALSE;
  13. static BOOL Capturing = FALSE;
  14.  
  15. long FAR PASCAL ToolbarProc (HWND, WORD, WORD, LONG);
  16. long FAR PASCAL ToolbarButtonProc (HWND, WORD, WORD, LONG);
  17.  
  18.  
  19. /* 3D Styles:
  20.  
  21.         0 = Normal 3D
  22.         1 = Normal 3D hole
  23.         2 = Normal 3D hole, white inside
  24. */
  25.  
  26. void Create3DEffect (HDC hdc, RECT *rect, int thickness, int style)
  27. {
  28.     int     i;
  29.     int     x1 = rect->left;
  30.     int     y1 = rect->top;
  31.     int     x2 = rect->right;
  32.     int     y2 = rect->bottom;
  33.     HBRUSH  hbrush, oldbrush;
  34.     HPEN    hpen, oldpen;
  35.  
  36.     oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 1, GetSysColor(COLOR_WINDOWFRAME)));
  37.  
  38.     switch (style) {
  39.         default:
  40.         case 0:
  41.         case 1: hbrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); break;
  42.         case 2: hbrush = GetStockObject(WHITE_BRUSH); break;
  43.     }
  44.  
  45.     oldbrush = SelectObject(hdc, hbrush);
  46.     Rectangle(hdc, x1, y1, x2, y2);
  47.  
  48.     switch (style) {
  49.         default:
  50.         case 0: hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
  51.                 break;
  52.         case 1: hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
  53.                 break;
  54.         case 2: hpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  55.                 break;
  56.     }
  57.     DeleteObject(SelectObject(hdc, hpen));
  58.  
  59.     for (i = 1; i <= thickness; i++) {
  60.         MoveTo(hdc, x1 + i, y1 + i);    LineTo(hdc, x1 + i, y2 - 1);
  61.         MoveTo(hdc, x1 + i, y1 + i);    LineTo(hdc, x2 - 1, y1 + i);
  62.     }
  63.  
  64.     switch (style) {
  65.         default:
  66.         case 0: hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
  67.                 break;
  68.         case 1: hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
  69.                 break;
  70.         case 2: hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
  71.                 break;
  72.     }
  73.     DeleteObject(SelectObject(hdc, hpen));
  74.  
  75.     for (i = 1; i <= thickness; i++) {
  76.         MoveTo(hdc, x1 + i, y2 - 1 - i);
  77.         LineTo(hdc, x2 - 1, y2 - 1 - i);
  78.         MoveTo(hdc, x2 - 1 - i, y2 - 2);
  79.         LineTo(hdc, x2 - 1 - i, y1 + i);
  80.     }
  81.  
  82.     SelectObject(hdc, oldbrush);
  83.     SelectObject(hdc, oldpen);
  84.     DeleteObject(hpen);
  85.  
  86.     switch (style) {
  87.         case 0:
  88.         case 1: DeleteObject(hbrush); break;
  89.         case 2: break;
  90.     }
  91. }
  92.  
  93.  
  94.  
  95. static void CursorShape (HWND hwnd, int state)
  96. {
  97.     HCURSOR hcursor;
  98.     static arrow = NULL;
  99.  
  100.     if (hwnd == NULL) return;
  101.  
  102.     if (state < 0) {
  103.         hcursor = (HCURSOR) GetWindowWord(GetParent(hwnd), 3 * sizeof(WORD));
  104.     } else {
  105.         if (arrow == NULL) {
  106.             hcursor = arrow = LoadCursor (NULL, IDC_ARROW);
  107.         } else {
  108.             hcursor = arrow;
  109.         }
  110.     }
  111.  
  112.     SetCursor(hcursor);
  113. }
  114.  
  115.  
  116. void EnableToolbarButton (HWND hwnd, int id, BOOL enabled)
  117. {
  118.     int             i, n;
  119.     TOOLBARICON     *icons;
  120.  
  121.     if (hwnd == NULL) return;
  122.  
  123.     icons = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  124.     n = (int) GetWindowWord(hwnd, sizeof(WORD));
  125.  
  126.     for (i = 0; i < n && icons[i].id != id; i++);
  127.  
  128.     if (i >= n) return;
  129.  
  130.     if (enabled) {
  131.         if (icons[i].state >= 0) return;
  132.         icons[i].state = icons[i].oldstate;
  133.     } else {
  134.         if (icons[i].state < 0) return;
  135.         icons[i].oldstate = icons[i].state;
  136.         icons[i].state = -1;
  137.     }
  138.     InvalidateRect(icons[i].hwnd, NULL, FALSE);
  139.     UpdateWindow(icons[i].hwnd);
  140.     //CursorShape (icons[i].hwnd, icons[i].state);
  141. }
  142.  
  143.  
  144.  
  145. HWND GetToolbarButton (HWND hwnd, int id, TOOLBARICON *ip)
  146. {
  147.     int             i, n;
  148.     TOOLBARICON     *icons;
  149.  
  150.     if (hwnd == NULL) return (NULL);
  151.  
  152.     icons = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  153.     n = (int) GetWindowWord(hwnd, sizeof(WORD));
  154.  
  155.     for (i = 0; i < n && icons[i].id != id; i++);
  156.  
  157.     if (i >= n) return (NULL);
  158.  
  159.     if (ip != NULL) *ip = icons[i];
  160.  
  161.     return (icons[i].hwnd);
  162. }
  163.  
  164.  
  165.  
  166. void ModifyToolbarButton (HWND hwnd, TOOLBARICON *icon)
  167. {
  168.     TOOLBARICON *old;
  169.  
  170.     if (hwnd == NULL) return;
  171.  
  172.     old = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  173.  
  174.     old->id = icon->id;
  175.     old->x = icon->x;
  176.     old->y = icon->y;
  177.     old->width = icon->width;
  178.     old->height = icon->height;
  179.     old->state = icon->state;
  180.     old->cycle = icon->cycle;
  181.     old->disabled = icon->disabled;
  182.     old->undepressed = icon->undepressed;
  183.     old->depressed = icon->depressed;
  184.     old->grayed = icon->grayed;
  185.     old->pressing = icon->pressing;
  186.  
  187.     InvalidateRect(hwnd, NULL, TRUE);
  188.     UpdateWindow(hwnd);
  189. }
  190.  
  191.  
  192.  
  193. HWND CreateToolbar (HWND parent, int x, int y, int width, int height,
  194.                     int thickness, int id, int n, HANDLE hInstance,
  195.                     TOOLBARICON *icons, char *xcursor)
  196. {
  197.     int         i;
  198.     HWND        hwnd;
  199.     WNDCLASS    wndclass;
  200.  
  201.     if (!ClassRegistered) {
  202.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  203.         wndclass.lpfnWndProc    = ToolbarProc;
  204.         wndclass.cbClsExtra     = 0;
  205.         wndclass.cbWndExtra     = 4 * sizeof(WORD);
  206.         wndclass.hInstance      = hInstance;
  207.         wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  208.         wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  209.         wndclass.hbrBackground  = NULL;
  210.         wndclass.lpszMenuName   = NULL;
  211.         wndclass.lpszClassName  = TOOLBARCLASS;
  212.  
  213.         RegisterClass(&wndclass);
  214.  
  215.         wndclass.cbWndExtra     = sizeof(WORD);
  216.         wndclass.hCursor        = NULL;
  217.         wndclass.lpfnWndProc    = ToolbarButtonProc;
  218.         wndclass.lpszClassName  = TOOLBARBUTTONCLASS;
  219.  
  220.         RegisterClass(&wndclass);
  221.     }
  222.  
  223.     hwnd = CreateWindow(TOOLBARCLASS, "", WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
  224.                         x, y, width, height, parent, id, hInstance, NULL);
  225.  
  226.     SetWindowWord(hwnd, 0, (WORD) icons);
  227.     SetWindowWord(hwnd, sizeof(WORD), (WORD) n);
  228.     SetWindowWord(hwnd, 2 * sizeof(WORD), (WORD) thickness);
  229.     if (xcursor != NULL) {
  230.         SetWindowWord(hwnd, 3 * sizeof(WORD), LoadCursor(hInstance, xcursor));
  231.     } else {
  232.         SetWindowWord(hwnd, 3 * sizeof(WORD), NULL);
  233.     }
  234.  
  235.     //ShowWindow(hwnd, SW_SHOW);
  236.  
  237.     /* Create the children */
  238.  
  239.     for (i = 0; i < n; i++) {
  240.         icons[i].oldstate = 0;
  241.         icons[i].hwnd = CreateWindow(TOOLBARBUTTONCLASS, "",
  242.                                      WS_CHILD | WS_VISIBLE,
  243.                                      icons[i].x, icons[i].y,
  244.                                      icons[i].width, icons[i].height,
  245.                                      hwnd, icons[i].id, hInstance, NULL);
  246.  
  247.         SetWindowWord(icons[i].hwnd, 0, (WORD) &(icons[i]));
  248.         //ShowWindow(icons[i].hwnd, SW_SHOW);
  249.     }
  250.  
  251.     return (hwnd);
  252. }
  253.  
  254.  
  255.  
  256. long FAR PASCAL ToolbarProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  257. {
  258.     int             i;
  259.     HDC             hdc;
  260.     PAINTSTRUCT     ps;
  261.     RECT            rect;
  262.     TOOLBARICON     *icons;
  263.  
  264.  
  265.     switch (message) {
  266.  
  267.         case WM_PAINT:
  268.             GetClientRect(hwnd, &rect);
  269.  
  270.             hdc = BeginPaint(hwnd, &ps);
  271.             Create3DEffect(hdc, &rect, GetWindowWord(hwnd, 2 * sizeof(WORD)), 0);
  272.             EndPaint(hwnd, &ps);
  273.             return (0);
  274.  
  275.         case WM_COMMAND:
  276.             SendMessage(GetParent(hwnd), WM_COMMAND,
  277.                         wParam | (GetWindowWord(hwnd, GWW_ID) << 8), lParam);
  278.             return (0);
  279.  
  280.         case BM_SETSTATE:
  281.         case BM_GETSTATE:
  282.             icons = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  283.             for (i = 0; icons[i].id != LOWORD(lParam); i++);
  284.             return (SendMessage(icons[i].hwnd, message, wParam, 0L));
  285.     }
  286.  
  287.     return (DefWindowProc(hwnd, message, wParam, lParam));
  288. }
  289.  
  290.  
  291.  
  292. long FAR PASCAL ToolbarButtonProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  293. {
  294.     int             i, j;
  295.     BOOL            crossed;
  296.     HDC             hdc, hdcmem;
  297.     PAINTSTRUCT     ps;
  298.     TOOLBARICON     *icon;
  299.     HBITMAP         hbitmap;
  300.     HANDLE            hInstance;
  301.  
  302.  
  303.     icon = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  304.  
  305.     switch (message) {
  306.  
  307.     case BM_SETSTATE:
  308.         if (icon->state == wParam) return (0);
  309.  
  310.         icon->oldstate = icon->state = wParam;
  311.         InvalidateRect(hwnd, NULL, FALSE);
  312.         UpdateWindow(hwnd);
  313.